package com.study.sfd.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.utils.PageUtils;
import com.study.common.utils.Query;
import com.study.sfd.medicine.dao.MedicineDao;
import com.study.sfd.medicine.entity.MedicineEntity;
import com.study.sfd.medicine.entity.MedicineImgsEntity;
import com.study.sfd.medicine.entity.SpecificationImgEntity;
import com.study.sfd.medicine.service.MedicineImgsService;
import com.study.sfd.medicine.service.MedicineService;
import com.study.sfd.medicine.service.SpecificationImgService;
import com.study.sfd.medicine.vo.MedicineWithDefaultImg;
import com.study.sfd.medicine.vo.MedicineWithImgs;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("medicineService")
public class MedicineServiceImpl extends ServiceImpl<MedicineDao, MedicineEntity> implements MedicineService {

  @Resource
  MedicineDao medicineDao;

  @Autowired
  MedicineImgsService medicineImgsService;

  @Autowired
  SpecificationImgService specificationImgService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<MedicineEntity> page = this.page(
        new Query<MedicineEntity>().getPage(params),
        new QueryWrapper<MedicineEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public PageUtils queryPageWithImg(Map<String, Object> params) {
    long currPage = 1;
    long pageSize = 10;

    if (!StringUtils.isEmpty(params.get("page"))) {
      currPage = Long.parseLong((String) params.get("page"));
    }
    if (!StringUtils.isEmpty(params.get("limit"))) {
      pageSize = Long.parseLong((String) params.get("limit"));
    }

    Page<MedicineWithDefaultImg> page = new Page<>(currPage, pageSize);
    medicineDao.getMedicineWithImg(page);
    return new PageUtils(page);
  }

  @Override
  @Transactional
  public void saveWithImgs(MedicineWithImgs medicineWithImgs) {
    MedicineEntity medicine = new MedicineEntity();
    BeanUtils.copyProperties(medicineWithImgs, medicine);
    this.save(medicine);

    Integer mdcId = medicine.getId();
    medicineWithImgs.getMedicineImgs().forEach(mdcImg -> mdcImg.setMedicineId(mdcId));
    medicineImgsService.saveBatch(medicineWithImgs.getMedicineImgs());

    medicineWithImgs.getSpecificationImg().forEach(specificationImgEntity -> specificationImgEntity.setMedicineId(mdcId));
    specificationImgService.saveBatch(medicineWithImgs.getSpecificationImg());
  }

  @Override
  public MedicineWithImgs getDetailById(Integer id) {
    MedicineWithImgs medicineWithImgs = new MedicineWithImgs();

    MedicineEntity medicine = this.getById(id);
    BeanUtils.copyProperties(medicine, medicineWithImgs);

    List<MedicineImgsEntity> medicineImgsEntities = medicineImgsService.lambdaQuery().eq(MedicineImgsEntity::getMedicineId, id).list();
    medicineWithImgs.setMedicineImgs(medicineImgsEntities);

    List<SpecificationImgEntity> specificationImgEntities = specificationImgService.lambdaQuery().eq(SpecificationImgEntity::getMedicineId, id).list();
    medicineWithImgs.setSpecificationImg(specificationImgEntities);

    return medicineWithImgs;
  }

  @Override
  @Transactional
  public void updateWithImgsById(MedicineWithImgs medicineWithImgs, Integer mdcId) {
    MedicineEntity medicine = new MedicineEntity();
    BeanUtils.copyProperties(medicineWithImgs, medicine);
    medicine.setId(mdcId);
    this.updateById(medicine);

    // 先根据 mdc id 删除，再批量保存
    medicineWithImgs.getMedicineImgs().forEach(mdcImg -> mdcImg.setMedicineId(mdcId));
    medicineImgsService.lambdaUpdate().eq(MedicineImgsEntity::getMedicineId, mdcId).remove();
    medicineImgsService.saveBatch(medicineWithImgs.getMedicineImgs());

    medicineWithImgs.getSpecificationImg().forEach(specificationImgEntity -> specificationImgEntity.setMedicineId(mdcId));
    specificationImgService.lambdaUpdate().eq(SpecificationImgEntity::getMedicineId, mdcId).remove();
    specificationImgService.saveBatch(medicineWithImgs.getSpecificationImg());
  }

  @Override
  @Transactional
  public void removeWithImgsByIds(List<Integer> ids) {
    for (Integer mdcId : ids) {
      this.removeById(mdcId);

      // 根据 mdc id 删除
      medicineImgsService.lambdaUpdate().eq(MedicineImgsEntity::getMedicineId, mdcId).remove();

      specificationImgService.lambdaUpdate().eq(SpecificationImgEntity::getMedicineId, mdcId).remove();
    }
  }

}